#include "LogManagerMenu.h"
#include "ui_LogManagerMenu.h"

LogManagerMenu::LogManagerMenu(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LogManagerMenu)
{
    ui->setupUi(this);

    LogManagerMenu::initMenu();
}

LogManagerMenu::~LogManagerMenu()
{
    delete ui;
}

void LogManagerMenu::insertLog(qint64 operationTime, QString operatorName, Role operatorRole, QString operationDescribe)
{
    QSqlQuery tmpQ;
    tmpQ.prepare("INSERT logEntry(operationTime,operatorName,operatorRole,operationDescribe) "
                 "VALUES(?,?,?,?)");
    tmpQ.addBindValue(operationTime);
    tmpQ.addBindValue(operatorName);
    tmpQ.addBindValue(static_cast<int>(operatorRole));
    tmpQ.addBindValue(operationDescribe);

    if(!tmpQ.exec())
    {
        throw std::runtime_error("添加日志记录语句执行失败");
    }
}

void LogManagerMenu::initMenu()
{
    ui->dte_find_operationTimeStart->setCalendarPopup(true);
    ui->dte_find_operationTimeEnd->setCalendarPopup(true);

    //筛选条件逻辑
    ui->le_find_operatorName->setEnabled(static_cast<bool>(ui->cb_operatorName->checkState()));
    connect(ui->cb_operatorName,&QCheckBox::stateChanged,this,[this](int state)
            {
                ui->le_find_operatorName->setEnabled(static_cast<bool>(state));
            });

    ui->le_find_operatorRole->setEnabled(static_cast<bool>(ui->cb_operatorRole->checkState()));
    connect(ui->cb_operatorRole,&QCheckBox::stateChanged,this,[this](int state)
            {
                ui->le_find_operatorRole->setEnabled(static_cast<bool>(state));
            });

    ui->dte_find_operationTimeStart->setEnabled(static_cast<bool>(ui->cb_operationTime->checkState()));
    connect(ui->cb_operationTime,&QCheckBox::stateChanged,this,[this](int state)
            {
                ui->dte_find_operationTimeStart->setEnabled(static_cast<bool>(state));
            });

    ui->dte_find_operationTimeEnd->setEnabled(static_cast<bool>(ui->cb_operationTime->checkState()));
    connect(ui->cb_operationTime,&QCheckBox::stateChanged,this,[this](int state)
            {
                ui->dte_find_operationTimeEnd->setEnabled(static_cast<bool>(state));
            });

    m_sqlQueryModel = new LogEntryManagerSqlQueryModel;
    m_sqlQueryModel->setQuery("SELECT * FROM logEntry");

    m_sqlQueryModel->setHeaderData(0,Qt::Horizontal,tr("日志ID"));
    m_sqlQueryModel->setHeaderData(1,Qt::Horizontal,tr("操作时间"));
    m_sqlQueryModel->setHeaderData(2,Qt::Horizontal,tr("操作人员姓名"));
    m_sqlQueryModel->setHeaderData(3,Qt::Horizontal,tr("操作人员角色"));
    m_sqlQueryModel->setHeaderData(4,Qt::Horizontal,tr("操作描述"));

    ui->tv_logInfoList->setSelectionBehavior(QAbstractItemView::SelectRows); // 单击选中整行
    ui->tv_logInfoList->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//宽度自动伸展
    ui->tv_logInfoList->verticalHeader()->setVisible(false);//不显示行号

    ui->tv_logInfoList->setModel(m_sqlQueryModel);

    connect(ui->tv_logInfoList,&QTableView::clicked,this,&LogManagerMenu::m_on_tv_logInfoList_clicked);

}


void LogManagerMenu::m_on_tv_logInfoList_clicked(const QModelIndex &index)
{
    ui->le_logId->setText(m_sqlQueryModel->data(this->m_sqlQueryModel->index(index.row(),0)).toString());
    ui->le_operationTIme->setText(m_sqlQueryModel->data(this->m_sqlQueryModel->index(index.row(),1)).toString());
    ui->le_operatorName->setText(m_sqlQueryModel->data(this->m_sqlQueryModel->index(index.row(),2)).toString());
    ui->le_operatorRole->setText(m_sqlQueryModel->data(this->m_sqlQueryModel->index(index.row(),3)).toString());
    ui->te_operationDescribe->setText(m_sqlQueryModel->data(this->m_sqlQueryModel->index(index.row(),4)).toString());
}
